﻿using B.S.Sterilizationcenter.Domain.zmc;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Zmc.Apparatu;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Drawing.Printing;

namespace B.S.Sterilizationcenter.Writer.API.Controllers.ZMC
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ApparatusController : ControllerBase
    {
        private readonly IMediator mediator;

        public ApparatusController(IMediator mediator)
        {
            this.mediator = mediator;
        }



        /// <summary>
        /// 添加器械数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>   
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> CreateApparatus(CreateApparatusCommends request, CancellationToken cancellationToken)
        {

            try
            {
                return await mediator.Send(request, cancellationToken);
            }
            catch (Exception)
            {

                throw;
            }

        }


        /// <summary>
        /// 创建设备类型
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> CreateApparatusType(CreateApparatusTypeCommends request, CancellationToken cancellationToken)
        {
            try
            {
                return await mediator.Send(request, cancellationToken);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<dynamic> UpFileImg(IFormFile file)
        {
            try
            {
                var fpath = "wwwroot/File/" + file.FileName;
                using (FileStream fs = new FileStream(fpath, FileMode.OpenOrCreate))
                {
                    file.CopyTo(fs);
                }
                return Ok("https://localhost:7286/File/" + file.FileName);
            }
            catch (Exception)
            {

                throw;
            }
        }



        /// <summary>
        /// 编辑器械数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        /// 
        [HttpPut]
        public async Task<ApiResult<int>> UpdataApparatus(UpdataApparatusCommends request, CancellationToken cancellationToken)
        {
            try
            {
                return await mediator.Send(request, cancellationToken);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除器械数据--逻辑删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public async Task<ApiResult<int>> DelApparatus(DelApparatusCommends request, CancellationToken cancellationToken)
        {
            try
            {

                return await mediator.Send(request, cancellationToken);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
